/*  Copyright (C) 2008  Alexander Blyzniuchenko (BlezAlex)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using Aga.Controls.Tree;
using DC;

namespace MediaDC
{
    class StatusNode : Node
    {
        private readonly ProgressData _data;
        private string _file;
        private string _user;
        private TaskStatus _status;
        private readonly Task _task;
        private DateTime _lastStatusUpdateTime = DateTime.Now;
        private long _prevPos;
        private string _speed;

        public StatusNode(Task t)
        {
            _data = new ProgressData();
            _task = t;

            Refresh();
            Value = 0;
        }

        public void Refresh()
        {
            _speed =
                FileItem.FormatFileSize(
                    (long) ((_task.Position - _prevPos)/(DateTime.Now - _lastStatusUpdateTime).TotalSeconds)) + "/sec";

            if (_task.Total == 0 || _task.Status == TaskStatus.Completed)
                _speed = string.Empty;
            
            if (_status != _task.Status || _prevPos != _task.Position)
            {
                _lastStatusUpdateTime = DateTime.Now;
            }

            _prevPos = _task.Position;
            _file = _task.File;
            _user = _task.User;
            _status = _task.Status;
            Text = string.Format("{0} {1} ({2}/{3})", _task.Status,_task.Text, 
                FileItem.FormatFileSize(_task.Position), 
                FileItem.FormatFileSize(_task.Total));
            
            if (_task.Total != 0)
                Value = (int) ((float)_task.Position /_task.Total*100);
        }

        public int Value
        {
            get { return _data.Position; }
            set { _data.Position = value; }
        }

        public ProgressData Progress
        {
            get { return _data; }
        }

        public override string Text
        {
            get { return _data.Text; }
            set { _data.Text = value; }
        }

        public string File
        {
            get { return _file;}
            set { _file = value;}
        }

        public string User
        {
            get { return _user; }
            set { _user = value; }
        }

        public TaskStatus Status
        {
            get { return _status; }
        }

        public DateTime LastStatusUpdateTime
        {
            get { return _lastStatusUpdateTime; }
        }

        public string Speed
        {
            get { return _speed; }
        }
    }
}